package com.zt.rongda.stock.service;

import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

import com.zt.rongda.stock.inter.StockPurchaseDetailMapper;
import com.zt.rongda.stock.inter.StockPurchaseMapper;
import com.zt.rongda.stock.model.StockPurchase;
import com.zt.rongda.stock.model.StockPurchaseDetail;
import com.zt.system.model.BusinessException;
import com.zt.servlet.ReqBean;
import com.zt.system.model.SqlBuilder;
import com.zt.system.service.BaseService;
import com.zt.util.DateUtil;
import com.zt.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class StockPurchaseService extends BaseService {

    @Autowired
    private StockPurchaseMapper stockPurchaseMapper;
    @Autowired
    private StockPurchaseDetailMapper stockPurchaseDetailMapper;

    /**
    * 查询
    *
    * @param reqBean
    * @return
    */
    public JSONObject getTable(ReqBean reqBean) {
        SqlBuilder sqlBuilder = new SqlBuilder("");
        sqlBuilder.appendSql("select a.*, b.EMP_NAME APPLICANT_NAME, c.EMP_NAME APPROVER_NAME from STOCK_PURCHASE a, EMPLOYEE b, EMPLOYEE c where a.APPLICANT = b.EMP_ID and a.APPROVER = c.EMP_ID ");
        sqlBuilder.addWhere("APPLICANT", reqBean.getInfo("applicant"));
        return queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
    }

    /**
    * 通过id查询
    *
    * @param listId
    * @return
    */
    public StockPurchase getById(String listId) {
        return stockPurchaseMapper.selectByPrimaryKey(listId);
    }

    /**
    * 新增
    *
    * @param stockPurchase
    */
    public void add(StockPurchase stockPurchase, ReqBean reqBean) {

        Date date = new Date();
        String listId = stringId();
        stockPurchase.setListId(listId);// 手动设置Id

        stockPurchase.setMakeTime(date);
        stockPurchase.setModifyTime(stockPurchase.getMakeTime());
        stockPurchaseMapper.insert(stockPurchase);

        String detail = reqBean.getInfo("detail");
        if(StringUtil.isNotEmpty(detail)){
            JSONArray jaDetail = JSONArray.fromObject(detail);

            if(jaDetail != null){

                Iterator<Object> it = jaDetail.iterator();
                for (int i = 0; it.hasNext(); i++) {

                    JSONObject joDetail = (JSONObject) it.next();
                    StockPurchaseDetail stockPurchaseDetail = new StockPurchaseDetail();

                    stockPurchaseDetail.setListId(listId);
                    stockPurchaseDetail.setStockId(joDetail.getString("stockId"));
                    stockPurchaseDetail.setStockName(joDetail.getString("stockName"));
                    stockPurchaseDetail.setTypeId(joDetail.getString("typeId"));
                    stockPurchaseDetail.setTypeName(joDetail.getString("typeName"));
                    stockPurchaseDetail.setSpec(joDetail.getString("spec"));
                    stockPurchaseDetail.setBrand(joDetail.getString("brand"));
                    stockPurchaseDetail.setUnit(joDetail.getString("unit"));
                    stockPurchaseDetail.setNum(Integer.parseInt(joDetail.getString("num")));
                    stockPurchaseDetail.setListId(listId);
                    stockPurchaseDetail.setOperator(reqBean.getUserId());
                    stockPurchaseDetail.setMakeTime(date);
                    stockPurchaseDetail.setModifyTime(date);

                    stockPurchaseDetailMapper.insert(stockPurchaseDetail);
                }
            }
        }
    }

    /**
    * 修改
    *
    * @param stockPurchase
    */
    public void update(StockPurchase stockPurchase, ReqBean reqBean) {
        String listId = stockPurchase.getListId();
        Date date = new Date();

        if (StringUtil.isEmpty(listId)) {
            throw new BusinessException("获取主键失败");
        }

        stockPurchase.setModifyTime(date);
        stockPurchaseMapper.updateByPrimaryKeySelective(stockPurchase);

        String detail = reqBean.getInfo("detail");
        if(StringUtil.isNotEmpty(detail)){
            JSONArray jaDetail = JSONArray.fromObject(detail);

            if(jaDetail != null){

                Iterator<Object> it = jaDetail.iterator();
                for (int i = 0; it.hasNext(); i++) {

                    JSONObject joDetail = (JSONObject) it.next();
                    StockPurchaseDetail stockPurchaseDetail = new StockPurchaseDetail();

                    stockPurchaseDetail.setListId(listId);
                    stockPurchaseDetail.setStockId(joDetail.getString("stockId"));
                    stockPurchaseDetail.setStockName(joDetail.getString("stockName"));
                    stockPurchaseDetail.setTypeId(joDetail.getString("typeId"));
                    stockPurchaseDetail.setTypeName(joDetail.getString("typeName"));
                    stockPurchaseDetail.setSpec(joDetail.getString("spec"));
                    stockPurchaseDetail.setBrand(joDetail.getString("brand"));
                    stockPurchaseDetail.setUnit(joDetail.getString("unit"));
                    stockPurchaseDetail.setNum(Integer.parseInt(joDetail.getString("num")));

                    String id = joDetail.optString("id", "");
                    if(StringUtil.isNotEmpty(id)){
                        stockPurchaseDetail.setId(id);
                        stockPurchaseDetail.setModifyTime(new Date());
                        stockPurchaseDetailMapper.updateByPrimaryKeySelective(stockPurchaseDetail);
                    }else{
                        stockPurchaseDetail.setId(stringId());
                        stockPurchaseDetail.setOperator(reqBean.getUserId());
                        stockPurchaseDetail.setMakeTime(date);
                        stockPurchaseDetail.setModifyTime(date);
                        stockPurchaseDetailMapper.insert(stockPurchaseDetail);
                    }
                }
            }
        }
    }

    /**
    * 删除
    *
    * @param listId
    */
    public void delete(String listId) {
        if (StringUtil.isEmpty(listId)) {
            throw new BusinessException("获取主键失败");
        }
        stockPurchaseMapper.deleteByPrimaryKey(listId);
    }

}